$(function () {
    InitTable();
    //根据窗口调整表格高度
    $(window).resize(function() {

        $('#mytab').bootstrapTable('resetView', {
            height: tableHeight()
        })
    });
    $("#searchDriverInfoBtn").click(function () {
       InitTable();
    });
    function InitTable() {
        $('#driverTab').bootstrapTable('destroy');
        $('#driverTab').bootstrapTable({
            url: "/driver/selectAllDriver",//数据源
            height: tableHeight(),//高度调整
            //search: true,//是否搜索
            pagination: true,//是否分页
            pageSize: 8,//单页记录数
            pageList: [5, 10, 20, 50],//分页步进值
            sidePagination: "client",//服务端分页
            contentType: "application/x-www-form-urlencoded",//请求数据内容格式 默认是 application/json 自己根据格式自行服务端处理
            dataType: "json",//期待返回数据类型
            method: "post",//请求方式
            //searchAlign: "right",//查询框对齐方式
            queryParamsType: "limit",//查询参数组织方式
            uniqueId: "s_id",
            queryParams: function getParams(params) {
                return {
                    s_card:$("#s_cardSearch").val(),
                    s_name:$("#s_nameSearch").val(),
                    s_sex:$("#s_sexSearch").val(),
                    s_intime:$("#s_intimeSearch").val(),
                    s_outtime:$("#s_outtimeSearch").val()
                }
            },
            searchOnEnterKey: false,//回车搜索
            //showRefresh: true,//刷新按钮
            //showColumns: true,//列选择按钮
            buttonsAlign: "right",//按钮对齐方式
            toolbar: "toolbar", //指定工具栏
            toolbarAlign: "right",//工具栏对齐方式
            columns: [
                {
                    title: "全选",
                    field: "select",
                    checkbox: true,
                    width: 20,//宽度
                    align: "center",//水平
                    valign: "middle"//垂直
                },
                {
                    field: "s_id",//键名
                    title: "id",//标题
                    sortable: true,//是否可排序
                    order: "desc"//默认排序方式
                },
                {
                    field: "s_card",
                    title: "员工编号",
                    algin:"center",
                    sortable: true,
                    titleTooltip: "this is name"
                },
                {
                    field: "s_name",
                    title: "姓名",
                    sortable: true

                },
                {
                    field: "s_sex",
                    title: "性别",
                    width:25,
                    align:"center",
                    sortable: true,
                    formatter: function (value, row, index) {
                        switch (value) {
                            case '1':
                                return '男';
                            case '2':
                                return '女';
                        }
                    }
                },
                {
                    field: "s_num",
                    title: "身份证号",
                    sortable: true
                },
                {
                    field: "s_address",
                    title: "家庭住址",
                    sortable: true
                },
                {
                    field: "state_name",
                    title: "状态",
                    width:30,
                    align:"center",
                    sortable: true
                },
                {
                    field: "s_intime",
                    title: "入职时间",
                    sortable: true
                },
                {
                    field: "s_outtime",
                    title: "离职时间",
                    sortable: true
                }

            ],
            onClickRow: function (row, $element) {
                //$element是当前tr的jquery对象
                $element.css("background-color", "#cecece");
            },//单击row事件
            locale: "zh-CN", //中文支持
            detailView: false, //是否显示详情折叠
            /*detailFormatter: function(index, row, element) {
                var html = '';
                $.each(row, function(key, val){
                    html += "<p>" + key + ":" + val +  "</p>"
                });
                return html;
            }*/
        });
    }
    function tableHeight() {
        return $(window).height() - 50;
    }
    $("#s_intimeSearch").datetimepicker({
        format:'yyyy-mm-dd hh:ii',
        local:moment.locale('zh-cn'),
        startDate:"2000-01-01 00:00",
        endDate:"2050-01-01 00:00"

    });
    $("#s_outtimeSearch").datetimepicker({
        format:'yyyy-mm-dd hh:ii',
        local:moment.locale('zh-cn'),
        startDate:"2000-01-01 00:00",
        endDate:"2050-01-01 00:00"

    });
    $("#s_intime").datetimepicker({
        format:'yyyy-mm-dd hh:ii',
        local:moment.locale('zh-cn'),
        startDate:"2000-01-01 00:00",
        endDate:"2050-01-01 00:00"

    });
    $("#s_outtime").datetimepicker({
        format:'yyyy-mm-dd hh:ii',
        local:moment.locale('zh-cn'),
        startDate:"2000-01-01 00:00",
        endDate:"2050-01-01 00:00"

    });
    $("#addDriverBtn").click(function () {
        var s_id = $("#s_id").val();
        if (s_id ===""){
            $("#myDialogHeaderLable").text("添加员工");
            $("#addRecord").text("添加");
        }
    });
    $("#updateDriverBtn").click(function () {
        $("#addModal").modal("show");
        $("#myDialogHeaderLable").text("更新员工");
        $("#addRecord").text("更新");
        var table = $("#driverTab").bootstrapTable("getSelections");
        if(table !=undefined && table.length === 1){
            var result = table[0];
            $("#s_id").val(result.s_id);
            $("#s_address").val(result.s_address),
            $("#s_intime").val(result.s_intime),
            $("#s_outtime").val(result.s_outtime),
            $("#s_card").val(result.s_card),
            $("#s_name").val(result.s_name),
            $("#s_sex").val(result.s_sex),
            $("#s_num").val(result.s_num)
        }
    });
    $("#addRecord").click(function () {
        var s_id = $("#s_id").val();
        if (s_id===""){
            $.ajax({
                url:"/driver/insertDriver",
                type:"post",
                dataType:"json",
                data:{
                    s_card:$("#s_card").val(),
                    s_name:$("#s_name").val(),
                    s_sex:$("#s_sex").val(),
                    s_num:$("#s_num").val(),
                    s_address:$("#s_address").val(),
                    s_state:$("#s_state").val(),
                    s_intime:$("#s_intime").val(),
                    s_outtime:$("#s_outtime").val()
                },
                success:function (result) {
                    var error = result.errorCode;
                    if (error == 0){
                        toastr.success(result.status);
                        $("#addModal").modal("hide");
                        $("#driverTab").bootstrapTable("refresh");
                        clearModelData();
                    }else {
                        toastr.error("插入失败")
                    }
                }
            })
        }else {
                $.ajax({
                    url:"/driver/updateDriver",
                    type:"post",
                    dataType:"json",
                    data: {
                        s_state:$("#s_state").val(),
                        s_card: $("#s_card").val(),
                        s_name: $("#s_name").val(),
                        s_sex: $("#s_sex").val(),
                        s_num: $("#s_num").val(),
                        s_address: $("#s_address").val(),
                        s_id: $("#s_id").val(),
                        s_intime: $("#s_intime").val(),
                        s_outtime: $("#s_outtime").val()
                    },
                    success:function (result) {
                        if (result.errorCode == 0){
                            toastr.success(result.status);
                            $("#addModal").modal("hide");
                            $("#driverTab").bootstrapTable("refresh");
                            clearModelData();
                        }else {
                            toastr.error("更新失败")
                        }
                    }
                })

        }
    });
    function clearModelData() {
        $("#s_id").val("");
        $("#s_address").val(""),
        $("#s_intime").val(""),
        $("#s_outtime").val(""),
        $("#s_card").val(""),
        $("#s_name").val(""),
        $("#s_sex").val(""),
        $("#s_num").val("")
    }
});
